var vm = new Vue({
    el:'#app',
    data:{
        username: '',
        password: '',
        captcha: '',
        error: false,
        errorMsg: '',
        src:g_comm.manager_url+'/captcha.jpg'
    },
    methods: {//定义方法
        //刷新验证码
        refreshCode: function(){
            this.src = g_comm.manager_url+"/captcha.jpg?t=" + $.now();
        },
        //登录
        login: function () {
            //校验表单输入项
            if(this.username==""){
                this.error = true;
                this.errorMsg = "请输入用户名";
                return;
            }
            if(this.password==""){
                this.error = true;
                this.errorMsg = "请输入密码";
                return;
            }
            if(this.captcha==""){
                this.error = true;
                this.errorMsg = "请输入验证码";
                return;
            }
            //显示loading框
            layer.load(0, { shade: [0.5, '#393D49'] ,zIndex :2147483647})
            //以对象的方式组装参数
            var data = {};
            data.username = this.username;
            data.password = this.password;
            data.captcha = this.captcha;
            //使用jquery发起ajax请求
            var _this = this; //临时存储vue对象实例，下面的ajax调用要用到
            $.ajax({
                url: g_comm.manager_url+"/sys/login",
                data: data,//需要传递到服务端的参数
                success: function(r){
                    layer.closeAll('loading');//关闭loading框
                    if(r.code == 0){//登录成功
                        //将登录token存放到localStorage
                        localStorage.setItem("speed-test-token", r.data.token);
                        location.href =g_comm.viewsPath+'/index.html';
                    }else{
                        _this.error = true;
                        _this.errorMsg = r.msg;
                        _this.refreshCode();
                    }
                }
            });
        }
    }
});